<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['construction:detail:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['construction:detail:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['construction:detail:remove']"
        >删除</el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="warning"-->
<!--          icon="el-icon-download"-->
<!--          size="mini"-->
<!--          @click="handleExport"-->
<!--          v-hasPermi="['construction:detail:export']"-->
<!--        >导出</el-button>-->
<!--      </el-col>-->
	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="detailList" @selection-change="handleSelectionChange" @row-dblclick="getStrategyCase" border >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="案例ID" align="center" prop="strategyDetailId" />
      <el-table-column label="施工攻略" align="center" prop="strategy.constructionStrategyTitle" :show-overflow-tooltip="true" />
      <el-table-column label="施工攻略标题" align="center" prop="strategyDetailTitle" :show-overflow-tooltip="true"/>
      <el-table-column label="项目名称" align="center" prop="strategyDetailName"  :show-overflow-tooltip="true"/>
      <el-table-column label="项目地点" align="center" prop="strategyDetailPlace"  :show-overflow-tooltip="true"/>
      <el-table-column label="项目盘楼" align="center" prop="strategyDetailFloor"  :show-overflow-tooltip="true"/>
      <el-table-column label="项目面积" align="center" prop="strategyDetailArea" />
      <el-table-column label="项目设计师" align="center" prop="strategyDetailDesigner" />
      <el-table-column label="最后修改时间" align="center" prop="modifiedTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.modifiedTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['construction:detail:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['construction:detail:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改施工攻略详情对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="施工攻略标题" prop="strategyDetailTitle">
          <el-input v-model="form.strategyDetailTitle" placeholder="请输入施工攻略标题" />
        </el-form-item>
        <el-form-item label="项目名称" prop="strategyDetailName">
          <el-input v-model="form.strategyDetailName" placeholder="请输入项目名称" />
        </el-form-item>
        <el-form-item label="项目地点" prop="strategyDetailPlace">
          <el-input v-model="form.strategyDetailPlace" placeholder="请输入项目地点" />
        </el-form-item>
        <el-form-item label="项目盘楼" prop="strategyDetailFloor">
          <el-input v-model="form.strategyDetailFloor" placeholder="请输入项目盘楼" />
        </el-form-item>
        <el-form-item label="项目面积" prop="strategyDetailArea">
          <el-input v-model="form.strategyDetailArea" placeholder="请输入项目面积" />
        </el-form-item>
        <el-form-item label="项目设计师" prop="strategyDetailDesigner">
          <el-input v-model="form.strategyDetailDesigner" placeholder="请输入项目设计师" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listDetail, getDetail, delDetail, addDetail, updateDetail, exportDetail } from "@/api/construction/detail";

export default {
  name: "Detail",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 施工攻略详情表格数据
      detailList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        constructionStrategyId: null,
      },
      // 施工案例ID
      constructionStrategyId:null,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        strategyDetailTitle: [
          { required: true, message: "施工攻略标题不能为空", trigger: "blur" }
        ],
        strategyDetailName: [
          { required: true, message: "项目名称不能为空", trigger: "blur" }
        ],
        strategyDetailPlace: [
          { required: true, message: "项目地点不能为空", trigger: "blur" }
        ],
        strategyDetailFloor: [
          { required: true, message: "项目盘楼不能为空", trigger: "blur" }
        ],
        strategyDetailArea: [
          { required: true, message: "项目面积不能为空", trigger: "blur" }
        ],
        strategyDetailDesigner: [
          { required: true, message: "项目设计师不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    let id = parseInt(this.$route.query.constructionStrategyId);
    this.constructionStrategyId = id;
    this.getList(id);
  },
  methods: {
    /** 查询施工攻略详情列表 */
    getList(constructionStrategyId) {
      this.loading = true;
      this.queryParams.constructionStrategyId = constructionStrategyId;
      listDetail(this.queryParams).then(response => {
        this.detailList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 案例说明
    getStrategyCase(row){
      this.$router.push({path:"/strategyCase/case",query:{strategyDetailId:row.strategyDetailId}});
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        strategyDetailId: null,
        constructionStrategyId: null,
        strategyDetailTitle: null,
        strategyDetailName: null,
        strategyDetailPlace: null,
        strategyDetailFloor: null,
        strategyDetailArea: null,
        strategyDetailDesigner: null,
        modifiedTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList(this.constructionStrategyId);
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.strategyDetailId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加施工攻略详情";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const strategyDetailId = row.strategyDetailId || this.ids
      getDetail(strategyDetailId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改施工攻略详情";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.strategyDetailId != null) {
            updateDetail(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList(this.constructionStrategyId);
              }
            });
          } else {
            this.form.constructionStrategyId = this.constructionStrategyId;
            addDetail(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList(this.constructionStrategyId);
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const strategyDetailIds = row.strategyDetailId || this.ids;
      this.$confirm('是否确认删除施工攻略详情编号为"' + strategyDetailIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delDetail(strategyDetailIds);
        }).then(() => {
          this.getList(this.constructionStrategyId);
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有施工攻略详情数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportDetail(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    }
  }
};
</script>
